# -*- coding: utf-8 -*-

#文件下载操作 expect_download()

from playwright.sync_api import sync_playwright

def run(playwright):
    chromium = playwright.chromium
    browser = chromium.launch(headless=False, slow_mo=3000)
    page = browser.new_page()
    page.goto(r'*************down.html')
    with page.expect_download() as download_info:
        page.get_by_text("点我下载").click()
    download = download_info.value
    # wait for download to complete
    print(download.url)  # 获取下载的url地址
    # 这一步只是下载下来，生成一个随机uuid值保存，代码执行完会自动清除
    print(download.path())
    # 最终可以用save_as 保存到本地
    download.save_as(download.suggested_filename)

    browser.close()


with sync_playwright() as playwright:
    run(playwright)


